﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.订单一览新.订单预览新.model;
using DXApplicationTangche.UC.订单一览新.辅料.辅料下单;
using DXApplicationTangche.UC.订单一览新.辅料.service;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.门店下单;
using DXApplicationTangche.UC.销售记录.service;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.门店下单.service;
using DXApplicationTangche.UC.货号管理;
using DXApplicationTangche.UC.门店下单.下单;

namespace DXApplicationTangche.UC.套号管理.套号下单
{
    public partial class Form套号销售样衣下单 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        //model
        private OrderModel orderModel = new OrderModel();
        private 订单预览新Model model;
        //变量
        private List<DTO辅料显示> DTO辅料显示s = new List<DTO辅料显示>();
        private Boolean if大货 = false;
        private String stockInventoryNo { get; set; }//样衣成衣码
        //工序显示工艺设计
        private XtraUserControl工序显示设计 uc工序显示设计 = new XtraUserControl工序显示设计();
        public Form套号销售样衣下单(String stockInventoryNo,DevExpress.XtraSplashScreen.SplashScreenManager splashScreenManager)
        {
            InitializeComponent();
            this.stockInventoryNo = stockInventoryNo;
            // 工艺设计
            this.uc工序显示设计 = new XtraUserControl工序显示设计();
            this.uc工序显示设计.Dock = DockStyle.Fill;
            this.xtraTabPage工艺设计.Controls.Clear();
            this.xtraTabPage工艺设计.Controls.Add(this.uc工序显示设计);
            // 查询orderCommitDto
            try
            {
                this.model = new 订单预览新Model(stockInventoryNo, Enum订单查询.按照水洗标查询订单);
            }
            catch (Exception)
            {
                MessageBox.Show("请扫描扫描成衣条码");
                return;
            }
            信息呈现();
            splashScreenManager.CloseWaitForm();
        }
        public Form套号销售样衣下单(String stockInventoryNo)
        {
            InitializeComponent();
            this.stockInventoryNo = stockInventoryNo;
            // 工艺设计
            this.uc工序显示设计 = new XtraUserControl工序显示设计();
            this.uc工序显示设计.Dock = DockStyle.Fill;
            this.xtraTabPage工艺设计.Controls.Clear();
            this.xtraTabPage工艺设计.Controls.Add(this.uc工序显示设计);
            // 查询orderCommitDto
            try
            {
                this.model = new 订单预览新Model(stockInventoryNo, Enum订单查询.按照水洗标查询订单);
            }
            catch (Exception)
            {
                MessageBox.Show("请扫描扫描成衣条码");
                return;
            }
            信息呈现();
        }
        public Form套号销售样衣下单(String stockInventoryNo,Boolean if大货, DevExpress.XtraSplashScreen.SplashScreenManager splashScreenManager)
        {
            InitializeComponent();
            this.stockInventoryNo = stockInventoryNo;
            this.if大货 = if大货;
            // 工艺设计
            this.uc工序显示设计 = new XtraUserControl工序显示设计();
            this.uc工序显示设计.Dock = DockStyle.Fill;
            this.xtraTabPage工艺设计.Controls.Clear();
            this.xtraTabPage工艺设计.Controls.Add(this.uc工序显示设计);
            // 查询orderCommitDto
            try
            {
                this.model = new 订单预览新Model(stockInventoryNo, Enum订单查询.按照水洗标查询订单);
            }
            catch (Exception)
            {
                MessageBox.Show("请扫描扫描成衣条码");
                return;
            }
            信息呈现();
            splashScreenManager.CloseWaitForm();
        }
        private void 信息呈现()
        {
            //显示成衣码查询出内容，无参考意义的进行清空
            this.model.buildOrderCommit().buildStyleCategoryDTOAndClear();
            if (!this.if大货)
            {
                //门店套号下单去除原客户，大货原客户信息
                this.model.buildCustomerClear();
                this.model.OrderCommitDTO返回.salesType = "套号下单";
                this.model.OrderCommitDTO返回.salesChannel = "春衫定制";
            }
            //款式
            this.gridControl款式.DataSource = this.model.款式DTOs;
            //设计点
            this.model.设计点DTOs.Clear();
            this.model.设计点DTOs = new XtraUserControlC设计点(this.model.OrderCommitDTO返回.styleNameCn).Dto设计点s;
            this.gridControl设计点.DataSource = this.model.设计点DTOs;
            //面料
            this.gridControl面料.DataSource = this.model.面料DTOs;
            // 辅料
            this.DTO辅料显示s.Clear();
            foreach (StyleAccessoryDTO item in this.model.OrderCommitDTO返回.styleAccessoryDTOs)
            {
                this.DTO辅料显示s.Add(new DTO辅料显示(item));
            }
            this.gridControl辅料.DataSource = this.DTO辅料显示s;
            this.advBandedGridView辅料.RefreshData();
            //显示该款式的尺码组
            this.comboBox尺码组.Items.Clear();
            List<String> 尺码组 = new List<string>();
            尺码组 = FunctionHelper.JsonDeserialization<List<String>>(RestCall.httpGetMethod(URL.接口url + "/size/findSizeCdByStyleNameCn?styleNameCn=" + this.model.OrderCommitDTO返回.styleNameCn));
            foreach (String str in 尺码组)
            {
                this.comboBox尺码组.Items.Add(str);
            }
            // 工艺设计
            this.uc工序显示设计.refreahData("", this.model.OrderCommitDTO返回.styleNameCn, true);
        }
        // 选择尺寸
        private void comboBox尺码组_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.model.OrderCommitDTO返回.styleSizeCd = "IGS_GROUP_SIZE-" + this.comboBox尺码组.Text.Trim();
            try
            {
                this.model.尺寸 =
                    FunctionHelper.JsonDeserialization<DTOStyleMeasureItems>(
                        RestCall.httpGetMethod(
                            URL.接口url + "/style/generateStyleMeasureItemsByStyleNameCn?SIZE_CD=" + this.model.OrderCommitDTO返回.styleSizeCd
                                        + "&styleNameCn=" + this.model.OrderCommitDTO返回.styleNameCn
                                        + "&categoryId=" + this.model.styleCategoryDTO.id
                                    )
                        );
                this.treeList尺寸.DataSource = this.model.尺寸.measureItemDTOS;
                this.treeList尺寸.Refresh();
            }
            catch
            {
                MessageBox.Show("无尺寸信息");
            }
        }
        private void numericUpDown数量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (((NumericUpDown)sender).Text.Trim().IndexOf('.') > -1)
            {
                if (((NumericUpDown)sender).Text.Trim().Substring(((NumericUpDown)sender).Text.Trim().IndexOf('.') + 1).Length >= 0)//定义小数位
                {
                    if (!char.IsDigit(e.KeyChar))
                    {
                        e.Handled = false;
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (String.IsNullOrEmpty(this.comboBox尺码组.Text))
            {
                MessageBox.Show("选择款式尺寸");
                return;
            }
            DialogResult dialogResult = MessageBox.Show("确认下单吗？", "下单", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dialogResult == DialogResult.Yes)
            {
                //保存orderCommit相关属性
                if (this.if大货)
                {
                    //大货客户名保留原来客户名
                    this.model.buildOrderCommitDTOSample(this.stockInventoryNo).buildDistributorOrderCommitDTO();
                }
                else
                {
                    //门店套号二次下单客户名为春衫
                    this.model.buildOrderCommitDTOSample(this.stockInventoryNo).buildSaleSampleOrderCommitDTO().build设计点();
                }
                this.model.OrderCommitDTO返回.orderNumber = Convert.ToDouble(this.numericUpDown数量.Value.ToString());
                this.orderModel.buildAddSaleSampleOrderCommitDto(this.model.OrderCommitDTO返回);
                DTO销售记录 销售记录 = new DTO销售记录();
                销售记录.build套号(orderModel.orderCommitDTO返回s);
                销售记录.build("50", "套号");
                销售记录Service.post销售记录(
                    销售记录,
                    Enum订单类别.套号订单
                );
                MessageBox.Show("保存成功");
                this.Close();
            }
        }

        private void richTextBox备注_TextChanged(object sender, EventArgs e)
        {
            this.model.OrderCommitDTO返回.remarks = this.richTextBox备注.Text.Trim();
        }
    }
}